package com.hfxt.service.impl;
import com.hfxt.constants.Constants;
import com.hfxt.dto.MaintainOrderDto;
import com.hfxt.mapper.MaintainOrderMapper;
import com.hfxt.mapper.PayOrderMapper;
import com.hfxt.po.MaintainOrder;
import com.hfxt.po.PayOrder;
import com.hfxt.service.MaintainOrderFlowService;
import com.hfxt.service.MaintainOrderService;
import com.hfxt.dto.Page;
import com.hfxt.service.PayOrderService;
import com.hfxt.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;


@Service
public class MaintainOrderServiceImpl implements MaintainOrderService {

    Logger log = LoggerFactory.getLogger(MaintainOrderServiceImpl.class);

    @Resource
    private MaintainOrderMapper maintainOrderMapper;

    @Autowired
    private MaintainOrderFlowService maintainOrderFlowService;

    @Autowired
    private PayOrderService payOrderService;

    @Resource
    private PayOrderMapper payOrderMapper;

    public MaintainOrder getMaintainOrderById(Long id){
        return maintainOrderMapper.getMaintainOrderById(id);
    }

    public List<MaintainOrder>	getMaintainOrderListByMap(Map<String,Object> param){
        return maintainOrderMapper.getMaintainOrderListByMap(param);
    }

    public Integer getMaintainOrderCountByMap(Map<String,Object> param){
        return maintainOrderMapper.getMaintainOrderCountByMap(param);
    }

    public Integer qdtxAddMaintainOrder(MaintainOrder maintainOrder){
            maintainOrder.setCreatdTime(new Date());
            return maintainOrderMapper.insertMaintainOrder(maintainOrder);
    }

    public Integer qdtxModifyMaintainOrder(MaintainOrder maintainOrder){
        maintainOrder.setUpdatedTime(new Date());
        return maintainOrderMapper.updateMaintainOrder(maintainOrder);
    }

    public Integer qdtxDeleteMaintainOrderById(Long id){
        return maintainOrderMapper.deleteMaintainOrderById(id);
    }

    public Integer qdtxBatchDeleteMaintainOrder(String ids){
        Map<String,List<String>> param=new HashMap<String,List<String>>();
        String[] paramArrays=ids.split(",");
        List<String> idList=new ArrayList<String>();
            for (String temp:paramArrays){
                idList.add(temp);
            }
        param.put("ids",idList);
        return maintainOrderMapper.batchDeleteMaintainOrder(param);
    }

    public Page<MaintainOrder> queryMaintainOrderPageByMap(Map<String,Object> param,Integer pageNo,Integer pageSize){
        Integer total = maintainOrderMapper.getMaintainOrderCountByMap(param);
        Page page = new Page(pageNo, pageSize, total);
        param.put("beginPos", page.getBeginPos());
        param.put("pageSize", page.getPageSize());
        List<MaintainOrder> maintainOrderList = maintainOrderMapper.getMaintainOrderListByMap(param);
        page.setList(maintainOrderList);
        return page;
     }

    /**
     * 查询维修订单详情
     *
     * @param orderNo
     * @return
     */
    @Override
    public MaintainOrderDto getByNo(String orderNo) {
        MaintainOrderDto maintainOrder = maintainOrderMapper.selectDtoByOrderNo(orderNo);
        return maintainOrder;
    }

    /**
     * 结束交易
     *
     * @param aliTradeNo  支付宝订单号
     * @param payNo       商户支付订单号
     * @param totalAmount
     * @return
     */
    @Override
    public int finishPay(String aliTradeNo, String payNo, String totalAmount) {
        int succ = 0;
        PayOrder payOrder = payOrderMapper.selectByPayNo(payNo);
        if (payOrder == null) {
            log.info("finishPay--->payNo:{},aliTradeNo:{}不存在", payNo, aliTradeNo);
            return succ;
        }
        String orderNo = payOrder.getOrderNo();
        MaintainOrder maintainOrder = maintainOrderMapper.selectByOrderNo(orderNo);
        Integer orderStatus = maintainOrder.getStatus();
        if (orderStatus == null || !orderStatus.equals(Constants.Order.MaintainStatus.APPOINT)) {
            log.info("订单orderNo:{},aliTradeNo:{}支付状态为:{},不可支付", orderNo, aliTradeNo, maintainOrder.getStatus());
            return succ;
        }
        // 1.修改订单状态
        if (orderStatus.equals(Constants.Order.MaintainStatus.APPOINT)) {
            maintainOrder.setStatus(Constants.Order.MaintainStatus.PAY_DEPOSIT);
        }
        maintainOrder.setPrice(new BigDecimal(totalAmount));
        Date currentDate = DateUtils.getCurrentDate(DateUtils.YYYY_MM_DD_HH_MM_SS);
        maintainOrder.setUpdatedTime(currentDate);
        succ = maintainOrderMapper.updateMaintainOrder(maintainOrder);
        // 2.修改支付订单状态
        payOrderService.finishPay(payOrder, aliTradeNo);
        // 3.保存订单流水信息
        maintainOrderFlowService.saveByMaintainOrder(maintainOrder);
        return succ;
    }


}
